{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A  B\n",
      "0  x  a\n",
      "1  y  b\n",
      "2  z  c\n",
      "原始数据 C:\n",
      "['x', 'y', 'x', 'z', 'y', 'a']\n",
      "\n",
      "替换后的数据 C:\n",
      "['a', 'b', 'a', 'c', 'b', None]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "# 示例一\n",
    "\n",
    "# 假设我们有一个 DataFrame\n",
    "data = {\n",
    "    'A': ['x', 'y', 'z'],\n",
    "    'B': ['a', 'b', 'c']\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "print(df)\n",
    "# 给定的另一组数据 C\n",
    "C = ['x', 'y', 'x', 'z', 'y', 'a']  # 示例数据\n",
    "\n",
    "# 创建一个映射字典\n",
    "mapping = dict(zip(df['A'], df['B']))\n",
    "\n",
    "# 使用 replace 方法进行替换\n",
    "C_replaced = [mapping.get(c) for c in C]\n",
    "\n",
    "# 打印结果\n",
    "print(\"原始数据 C:\")\n",
    "print(C)\n",
    "print(\"\\n替换后的数据 C:\")\n",
    "print(C_replaced)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    A   B  C\n",
      "0  a1  b1  1\n",
      "1  a1  b2  2\n",
      "2  a2  b3  3\n",
      "3  a3  b4  4\n",
      "4  a4  b5  5 \n",
      " -------------------------------------------\n",
      "    A   B  D\n",
      "0  a1  b1  4\n",
      "1  a1  b2  5\n",
      "2  a2  b3  6 \n",
      " -------------------------------------------\n",
      "    A   B  result\n",
      "0  a1  b1       4\n",
      "1  a1  b2      10\n",
      "2  a2  b3      18\n"
     ]
    }
   ],
   "source": [
    "# 示例二\n",
    "\n",
    "# 假设 plant_plan 和 plant_data 是已定义的 DataFrame\n",
    "# 示例数据\n",
    "plant_plan = pd.DataFrame({\n",
    "    'A': ['a1', 'a1', 'a2','a3','a4'],\n",
    "    'B': ['b1', 'b2', 'b3','b4','b5'],\n",
    "    'C': [1, 2, 3, 4, 5,]\n",
    "})\n",
    "\n",
    "print(plant_plan,'\\n',\n",
    "      '-------------------------------------------')\n",
    "plant_data = pd.DataFrame({\n",
    "    'A': ['a1', 'a1', 'a2'],\n",
    "    'B': ['b1', 'b2', 'b3'],\n",
    "    'D': [4, 5, 6]\n",
    "})\n",
    "\n",
    "print(plant_data,'\\n',\n",
    "      '-------------------------------------------')\n",
    "# 合并两个 DataFrame\n",
    "merged_df = pd.merge(plant_plan, plant_data, on=['A', 'B'])\n",
    "\n",
    "# 计算 c_i * d_j 的值\n",
    "merged_df['result'] = merged_df['C'] * merged_df['D']\n",
    "\n",
    "# 输出结果\n",
    "print(merged_df[['A', 'B', 'result']])\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
